<?php

use Workerman\Worker;
use MediaServer\Http\HttpWMServer;
use MediaServer\Rtmp\RtmpStream;
use MediaServer\Utils\WMBufferStream;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/src/functions.php';


$rtmpServer = new Worker('tcp://0.0.0.0:1935');
$rtmpServer->onConnect = function (\Workerman\Connection\TcpConnection $connection) {
    logger()->info("connection" . $connection->getRemoteAddress() . " connected . ");
    new RtmpStream(
        new WMBufferStream($connection)
    );
};
$rtmpServer->onWorkerStart = function ($worker) {
    logger()->info("rtmp server " . $worker->getSocketName() . " start . ");
    \MediaServer\Http\HttpWMServer::$publicPath = __DIR__ . '/public';
    $httpServer = new HttpWMServer("\\MediaServer\\Http\\ExtHttpProtocol://127.0.0.1:18080");
    $httpServer->listen();
    logger()->info("http server " . $httpServer->getSocketName() . " start . ");
};


$customProcess = new Worker();
$customProcess->count = 1;
$customProcess->name = 'TaskWorker';
$customProcess->onWorkerStart = function ($customProcess) {
    logger()->info("Custom process start.");
    $http = new Workerman\Http\Client();
    $http->get('https://example.com/', function ($response) {
        var_dump($response->getStatusCode());
        echo $response->getBody();
    }, function ($exception) {
        echo $exception;
    });
};

\Workerman\Worker::runAll();
